﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Data.SqlClient;

public partial class Pictures_UploadPictures : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
     try
        {
            string strGuid = "";
            string strPicExtension = "";
            string strFileName = "";

            // Firstly upload the picture associated with the news 
            if (FileUpload1.HasFile)
            {
                // Constructs the picture extension 
                strGuid = fnGuid();
                strPicExtension = fnGetPictureExtension(FileUpload1.FileName);
                strFileName = strGuid + strPicExtension;

                // Add picture to folder 
                subAddNewPicture(FileUpload1, strFileName, "Gallery");

                // Add the record to the database 
                UpdateGallery(cmbCategory.SelectedValue, strFileName, txtMemo.Text.Trim());

                lblUploadMessage.Text = "Картинката беше качена успешно...";
            }
            else
            {
                lblUploadMessage.Text = "Моля изберете подхудящя картинка...";
            }

            lblUploadMessage.Visible = true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

        private void UpdateGallery(string strCategoryValue, string strFileName, string strMemo)
    {
        // Variables declaration 
        string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ClinicConnectionString"].ConnectionString;
        SqlConnection sqlConn = new SqlConnection(strConnString);
        SqlCommand sqlcmd = new SqlCommand();

        // Building the query 
        string sqlQuery_Photo = "INSERT INTO [Photo] VALUES ('{0}', '{1}', '{2}', '{3}')";

        sqlQuery_Photo = sqlQuery_Photo.Replace("{0}", strCategoryValue);
        sqlQuery_Photo = sqlQuery_Photo.Replace("{1}", strFileName);
        sqlQuery_Photo = sqlQuery_Photo.Replace("{2}", strMemo);
        sqlQuery_Photo = sqlQuery_Photo.Replace("{3}", "18");

        // Retrieving search result 
        sqlConn.Open();
        sqlcmd.Connection = sqlConn;

        // Adding records to table 
        sqlcmd.CommandText = sqlQuery_Photo;
        sqlcmd.ExecuteNonQuery();

        sqlConn.Close();
    }


    // Adds picture to specified folder 
    private void subAddNewPicture(FileUpload FileUploader, string strPictureName, string strImgFolder)
    {
        string strImageFolderPath;
        string strImagePath;

        try
        {
            // Construct saving path 
            strImageFolderPath = Path.Combine(Request.PhysicalApplicationPath, strImgFolder);
            strImagePath = Path.Combine(strImageFolderPath, strPictureName);

            // Upload image 
            FileUploader.SaveAs(strImagePath);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    // Function to generate a GUID using current date and time 
    private string fnGuid()
    {
        string strGuid = "";

        try
        {
            {
                strGuid = DateTime.Today.Year.ToString() + DateTime.Today.Month.ToString() + DateTime.Today.Day.ToString()
                    + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
                
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

        // returning the guid generated 
        return strGuid;
    }

    // Returns true if file name has the extension .jpg, .gif, .jpeg 
    private string fnGetPictureExtension(string strPictureName)
    {
        try
        {
            {
                if (strPictureName.ToUpper().EndsWith(".JPG"))
                {
                    return ".JPG";
                }
                else if (strPictureName.ToUpper().EndsWith(".GIF"))
                {
                    return ".GIF";
                }
                else if (strPictureName.ToUpper().EndsWith(".JPEG"))
                {
                    return ".JPEG";
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

        // Else if has no extension so it returns "" 
        return "";
    }
}